<?php
declare(strict_types=1);

namespace app\api\service;

use app\common\library\Pay;
use app\common\model\delivery\CouponList;
use app\common\model\delivery\PayLog;
use app\common\model\delivery\ShopGoods;
use app\common\model\delivery\ShopOrder as ShopOrderModel;
use app\common\model\UserLog;
use app\common\service\BaseService;
use think\facade\Db;

class ShopOrder extends BaseService
{
    private $goods_id;
    private $number;
    private $user;
    private $platform;
    private $customer_id;

    public function createOrder()
    {
        $goods=$this->getObj(ShopGoods::class);
        [$total_score,$total_money]=$this->formateScoreAndMoney();
        if($total_score>$this->user['score']){
            throw new \Exception('积分不足！');
        }
        if($goods->goods_type==ShopGoods::GOODSTYPE('实体商品') && $goods->stock<$this->number){
            throw new \Exception('商品库存不足！');
        }
        if(
            $goods->goods_type==ShopGoods::GOODSTYPE('卡券商品') &&
            $goods->coupon->total &&
            $goods->coupon->total-$goods->coupon->send<$this->number
        ){
            throw new \Exception('商品库存不足！');
        }
        $data=[];
        $data['channel_id']=$goods->channel_id;
        $data['user_id']=$this->user['id'];
        $data['customer_id']=$this->customer_id;
        $data['goods_id']=$this->goods_id;
        $data['order_no']=create_out_trade_no();
        $data['number']=$this->number;
        $data['price']=$goods->price;
        $data['total_money']=$total_money;
        $data['total_score']=$total_score;
        $data['is_pay']=0;
        if($total_money==0){
            $data['is_pay']=1;
        }
        $payinfo='';
        Db::startTrans();
        try{
            $order=new ShopOrderModel();
            $order->save($data);
            if($total_score>0 && $total_money==0){
                UserLog::addScoreLog($this->user['id'],'minus',$total_score,$data['order_no'],'积分商城购买商品【'.$goods['name'].'】');
                $goods->sale=$goods->sale+$order->number;
                if($goods->goods_type==ShopGoods::GOODSTYPE('实体商品')){
                    $goods->stock=$goods->stock-$order->number;
                }
                $goods->save();
                if($goods->goods_type==ShopGoods::GOODSTYPE('卡券商品')){
                    CouponList::send($this->customer_id,$goods->coupon,$order->number,'积分商城购买商品【'.$goods['name'].'】');
                }
            }
            if($total_money>0){
                $payinfo=(new Pay($goods->channel->sub_mch_id))->unifiedorder($order->order_no,$this->user['id'],$order->total_money,'用户下单购买-'.$goods['name'],PayLog::TYPE('支付积分商城订单'),$this->platform);
            }
            Db::commit();
        }catch (\Exception $e) {
            Db::rollback();
            throw new \Exception($e->getMessage());
        }
        return $payinfo;
    }

    private function formateScoreAndMoney()
    {
        $goods=$this->getObj(ShopGoods::class);
        $score=$goods->max_score_money*$goods->score_price*$this->number;
		$score=($score>$this->user['score'])?$this->user['score']:$score;
        if($goods->score_price){
            $money=$goods->price*$this->number-$score/$goods->score_price;
        }else{
            $money=$goods->price*$this->number;
        }
        return [$score,$money];
    }

    protected function init()
    {
        $goods=ShopGoods::with(['channel','coupon'])->find($this->goods_id);
        if(!$goods || $goods['status']!='normal'){
            throw new \Exception('商品不存在或者已经下架！');
        }
        $this->setObj(ShopGoods::class,$goods);
    }
}